package ElectricCraft.misc;

import javax.swing.*;
import java.awt.*;

/**
 * GridBagPanel class supplies better interface for GridBagLayout.
 */
public class GridBagPanel extends JPanel {
    public GridBagPanel() {
        super(new GridBagLayout());
    }

    private final Insets insets = new Insets(0, 0, 0, 0);
    public void setInsets(int top, int right, int bottom, int left) {
        insets.top      = top;
        insets.right    = right;
        insets.left     = left;
        insets.bottom   = bottom;
    }

    private float weightX = 1.0f;
    private float weightY = 1.0f;
    public void setWeights(float x, float y) {
        weightX = x;
        weightY = y;
    }

    private int gridWidth = 1;
    private int gridHeight = 1;
    public void setGridSize(int width, int height) {
        gridWidth = width;
        gridHeight = height;
    }

    public void reset() {
        insets.left = insets.right = 0;
        insets.top  = insets.bottom = 0;
        weightX = weightY = 1.f;
        gridWidth = gridHeight = 1;
    }

    private void add(int x, int y, int fill, Component c) {
        GridBagConstraints constr = new GridBagConstraints();
        constr.gridx = x;
        constr.gridy = y;
        constr.gridwidth  = gridWidth;
        constr.gridheight = gridHeight;
        constr.weightx = weightX;
        constr.weighty = weightY;
        constr.insets = insets;
        constr.fill  = fill;

        this.add(c, constr);

        reset();
    }

    public void addFill(int x, int y, Component c) {
        add(x, y, GridBagConstraints.BOTH, c);
    }

    public void addHorizontal(int x, int y, Component c) {
        setWeights(weightX, 0);
        add(x, y, GridBagConstraints.HORIZONTAL, c);
    }
}
